package xmltest;

import java.io.IOException;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Main {
	public static final String[] nodeTypes = new String[]{"Element", "Attribute", "Text", "CData Section", "Entity Reference", "Entity",
			"Processing Instruction", "Comment", "Document", "Document Type", "Document Fragment", "Notation"
	};
	
	public static void main(String[] args){
		String document = "xml/styles.xml";
		
		DOMParser parser = new DOMParser();
		try{
			parser.parse(document);
			System.out.println(document + " is well-formed.");
			traverse(parser.getDocument());
		} catch(SAXException e){
			System.out.println(document + " is not well-formed.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void traverse(Node n){
		//if(n.getNodeType()==3)
			//return;
		System.out.println(nodeTypes[n.getNodeType()-1]+": "+n.getNodeName());
		if(n.hasAttributes()){
			for(int i = 0; i<n.getAttributes().getLength(); i++){
				Node att = n.getAttributes().item(i);
				System.out.println(nodeTypes[att.getNodeType()-1]+": "+att.getNodeName()+"="+att.getNodeValue());
			}
		}
		if(n.hasChildNodes()){
			System.out.println(n.getNodeName()+"'s Children: ");
			for(int i = 0; i<n.getChildNodes().getLength(); i++){
				traverse(n.getChildNodes().item(i));
			}
			System.out.println();
		}
	}
	
	
}
